﻿/******************************************************************
*
* ListBuilder
* Just work for Cn.Cellula Business Framework
* Autor:  AnyRock@mending.cn
* Create: 2011-5-4
* Last:   2011-5-4
*
******************************************************************/
var ListBuilder = function(rHd){
    var __hd = (typeof(rHd)=="string")?rHd:"productList";
    var __listData = null;
    var __listBox = null;
    var __ajax = null;
    var __marketKey = null;
    var __categoryKey = null;
    var __templateKey = null;
    var __direct = 0;//0:v,1:h
    var __userTemplate = null;
    var __requestUrl = null;
    var __repeatColumns = 1;
    var __topCount = 0;
    var __template = "";
    
    var __showLoading = function(){
        __listBox.innerHTML = '<div style="	background-image:url(/cellula_client/Controls/images/loading.gif);background-position:center center;background-repeat:no-repeat;width:40px;height:40px;overflow:hidden;">&nbsp;</div>';   
    };
    
    var __loadData = function(){
        if(__requestUrl==null){
            alert("未设定数据源");
            return;
        }
        if(__topCount>0){
            if(__requestUrl.indexOf("?")!=-1){
                __requestUrl += "&top="+__topCount;
            }else{
                __requestUrl += "?top="+__topCount;
            }
            __requestUrl += "&rm="+jskitUtil.guid();
        }
        __ajax = new JskitXmlHttpAction(__requestUrl,__hd+".__loadDataCallback","text");
        __showLoading(); 
    };
    
    this.__loadDataCallback = function(json){
        __ajax = null;
        __listBox.innerHTML = "";
        try{
            eval("__listData = "+json);
        }catch(e){
            __listBox.innerHTML = "[Load data error:]\n"+e.message;
            return;
        }
        if(__listData==null){return;}
        __loadTemplate();
        __showLoading();
        
    };
    
    
    
    var __loadTemplate = function(){
        var _url = "Ws/_Template.aspx?id="+__templateKey + "&rm="+jskitUtil.guid();
        __ajax = new JskitXmlHttpAction(_url,__hd+".__loadTemplateCallback","text");
        _url = null;
    }
    
    this.__loadTemplateCallback = function(tlContent){
        __ajax = null;
        __listBox.innerHTML = "";
        __template = tlContent;
        __buildContent();
    };
    
    this.setTemplate = function(rContent,rTitle){
        __template = rContent;
    };

    this.setRequestUrl = function(v){
        __requestUrl = v;
    };
    
    this.setRepeatColumns = function(v){
        __repeatColumns = (!isNaN(parseInt(v)))?parseInt(v):1;
        if(__repeatColumns<1){
            __repeatColumns = 1;
        }
    };
    
    this.load = function(){
        __loadData();
    };
    this.deploy = function(rBoxId,rRequestUrl,rTemplateId,rTopCount){
        __listBox = $("#"+rBoxId);
        if(typeof(rRequestUrl)=="string" && rRequestUrl!=""){
            __requestUrl = rRequestUrl;
        }
        if(typeof(rTemplateId)=="string" && rTemplateId!=""){
            __templateKey = rTemplateId;
        }
        try{
            if(typeof(rTopCount)=="number" && rTopCount>0){
                __topCount = parseInt(rTopCount);
            }
        }catch(e){
            __topCount = 0;
        }
        __loadData();
    };
    var __buildContent = function(){
        var _str = new Array();
        var _eo = null;
        var _item = null;
        _str.push('<table cellspacing="0" cellpadding="0" border="0"><tr>')
        for(var i=0;i<__listData.length;i++){
            _item = __template;
            _eo = __listData[i];
            _str.push('<td align="center">');
            if(typeof(_eo.ProductId)=="string"){
                _str.push(__parseProductEO(_eo,_item));
            }else if(typeof(_eo.NewsKey)=="string"){
                _str.push(__parseNewsEO(_eo,_item));
            }
            _str.push('</td>');
            if((i+1)%__repeatColumns==0){
                _str.push('</tr><tr>');
            }
            _item = _eo = null;
        }
        _str.push('</tr></table>');
        __listBox.innerHTML = _str.join('');
        _str = null;
    };
    var __parseProductEO = function(rEO,rTl){
        if(rEO.CoverPath==""){
            rTl = rTl.replace(/\$CoverPath/gi,"/cellula_client/default/images/def_product_cover.png");
        }else{
            rTl = rTl.replace(/\$CoverPath/gi,rEO.CoverPath);
        }
        if(rEO.Thumbnail==""){
            rTl = rTl.replace(/\$Thumbnail/gi,"/cellula_client/default/images/def_product_cover.png");
        }else{
            rTl = rTl.replace(/\$Thumbnail/gi,rEO.Thumbnail);
        }

        rTl = rTl.replace(/\$ProductId/gi,rEO.ProductId);
        rTl = rTl.replace(/\$ProductName/gi,rEO.ProductName);
        rTl = rTl.replace(/\$MarketName/gi,rEO.MarketName);
        rTl = rTl.replace(/\$Length/gi,rEO.Length*1000);
        rTl = rTl.replace(/\$Width/gi,rEO.Width*1000);
        rTl = rTl.replace(/\$Height/gi,rEO.Height*1000);
        rTl = rTl.replace(/\$Material/gi,rEO.Material);
        rTl = rTl.replace(/\$Color/gi,rEO.Color);
        rTl = rTl.replace(/\$ClickCount/gi,rEO.ClickCount);
        rTl = rTl.replace(/\$RetailPrice/gi,rEO.RetailPrice);
        rTl = rTl.replace(/\$PreferentialPrice/gi,rEO.PreferentialPrice);
        rTl = rTl.replace(/\$GroupPrice/gi,rEO.GroupPrice);
        rTl = rTl.replace(/\$TransCost/gi,rEO.TransCost);
        rTl = rTl.replace(/\$ProductCount/gi,rEO.ProductCount);
        rTl = rTl.replace(/\$ProductModel/gi,rEO.ProductModel);
        rEO = null;
        return rTl; 
    };
    var __parseNewsEO = function(rEO,rTl){
        rTl = rTl.replace(/\$NewsKey/gi,rEO.NewsKey);
        rTl = rTl.replace(/\$Title/gi,rEO.Title);
        rEO = null;
        return rTl; 
    };

    //BEGIN! category list ===================================================================
    var __cssClassPrefix = "pcl";
    var __loadCategoryData = function(){
        var _url = __requestUrl;
        if(_url.indexOf("?")==-1){
            _url += "?rm="+jskitUtil.guid();
        }else{
            _url += "&rm="+jskitUtil.guid();
        }
        __ajax = new JskitXmlHttpAction(_url,__hd+".__loadCategoryDataCallback","text");
        __showLoading(); 
    };
    this.__loadCategoryDataCallback = function(json){
        __ajax = null;
        __listBox.innerHTML = "";
        try{
            eval("__listData = "+json);
        }catch(e){
            __listBox.innerHTML = "[Load data error:]\n"+e.message;
            return;
        }
        if(__listData==null){return;}
        __listBox.innerHTML = __buildCategoryTree();
    };
    var __deep = 0;
    var __buildTreeNode = function(rNode,rLayer){
        var _str = new Array();
        _str.push('<div class="'+__cssClassPrefix+'_item">');
        var _url = window.location.href;
        var _link = __linkUrl.replace(/\$1/gi,rNode[0]);
        var _itemcss = (rLayer>0)?__cssClassPrefix+"_item_l":__cssClassPrefix+"_item_f";
        if(rNode.length>2 && rNode[2].length>0){
            if(_url.indexOf(rNode[0])!=-1){
                _str.push('<div class="'+_itemcss+'_a" ><a href="'+_link+'">'+rNode[1]+'</a></div>');
            }else{
                _str.push('<div class="'+_itemcss+'" ><a href="'+_link+'">'+rNode[1]+'</a></div>');
            }
            _str.push('<div class="'+__cssClassPrefix+'_sub">');
            rLayer++;
            for(var i=0;i<rNode[2].length;i++){
                _str.push(__buildTreeNode(rNode[2][i],rLayer));
            }
            _str.push('</div>');
        }else{
            if(_url.indexOf(rNode[0])!=-1){
                _str.push('<div class="'+_itemcss+'_a" ><a href="'+_link+'">'+rNode[1]+'</a></div>');
            }else{
                _str.push('<div class="'+_itemcss+'" ><a href="'+_link+'">'+rNode[1]+'</a></div>');
            }
        }
        _str.push('</div>');
        _link = _url = null;
        return _str.join('');
    };
    var __buildCategoryTree = function(){
        var _str = new Array();
        for(var i=0;i<__listData.length;i++){
            _str.push(__buildTreeNode(__listData[i],0));            
        }
        return _str.join('');
    };
    var __linkUrl = null;
    this.setLinkUrl = function(v){
        __linkUrl = v;
    };
    this.setCssClassPrefix = function(v){
        __cssClassPrefix = v;
    };
    this.deployCategoryList = function(rBoxId,rRequestUrl,rLinkUrl){
        __listBox = $("#"+rBoxId);
        __requestUrl = rRequestUrl;
        __linkUrl = (typeof(rLinkUrl)=="string")?rLinkUrl:"#";
        __loadCategoryData();
    };
    //END! product category list======================================================================
    
};